﻿/*
* ---------------------------------------------------
* 作者: MTQ 
* 日期： 2020/9/25 19:39:51 
* ---------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MTQ.Model;
using MTQ.BLL;
using MTQ.IBLL;
using MTQ.Common;

namespace MTQ.Web.Controllers
{

    public class Base_ButtonController : BaseController
    {

        private IBase_ButtonService ibase_buttonService = new Base_ButtonService();


        //                                           
        // GET: //         
        [AccountAuth(Roles = "Base_Button_Index")]
        public ActionResult Index()
        {
            return View();
        }


        [AccountAuth(Roles = "Base_Button_Index")]
        public ActionResult GetActionBution()
        {
            Base_ButtonService buttonService = new Base_ButtonService();
            Base_ButtonQuery base_buttonQuery = new Base_ButtonQuery();

            var servertBootStrapTable = buttonService.LoadSearchData(base_buttonQuery);

            return Json(servertBootStrapTable, JsonRequestBehavior.AllowGet);
        }




        //          
        // GET: /Base_Button/Create          
        [AccountAuth(Roles = "Base_Button_Create")]
        public ActionResult Create()
        {
            Base_Button base_button = new Base_Button();
            base_button.Button_Id = System.Guid.NewGuid();
            return View(base_button);
        }

        //          
        // POST: /Base_Button/Create          
        [HttpPost]
        [AccountAuth(Roles = "Base_Button_Create")]
        public ActionResult Create(Base_Button base_button)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                var result = ibase_buttonService.AddEntity(base_button);
                listMsg.Add(new ModelMessage("ChangeResult", "m001", result != null ? "保存成功" : "保存失败"));
            }
            return Json(listMsg);
        }

        [AccountAuth(Roles = "Base_Button_View")]
        public ActionResult MenuAction()
        {
            List<MTQ.Model.Base_Menu> listMenu = new Base_MenuService().MenuAll(null, 1);

            List<MenuButton> list = new Base_ButtonService().GetAuthByMenu(listMenu);

            return Json(list, JsonRequestBehavior.AllowGet);
        }


        [AccountAuth(Roles = "Base_Button_Edit")]
        public ActionResult Edit(Guid? primarykey)
        {
            Base_Button base_button = ibase_buttonService.LoadEntities(x => x.Button_Id == primarykey).FirstOrDefault();
            if (base_button == null)
            {
                return HttpNotFound();
            }
            return View(base_button);
        }

        [HttpPost]
        [AccountAuth(Roles = "Base_Button_Edit")]
        public ActionResult Edit(Base_Button formbase_button)
        {
            List<ModelMessage> listMsg = new List<ModelMessage>();
            if (ModelState.IsValid)
            {
                try
                {
                    Base_Button base_button = ibase_buttonService.LoadEntities(x => x.Button_Id == formbase_button.Button_Id).First();
                    base_button.Button_Id = formbase_button.Button_Id;
                    base_button.Button_Name = formbase_button.Button_Name;
                    base_button.Button_Code = formbase_button.Button_Code;
                    base_button.Button_Title = formbase_button.Button_Title;
                    base_button.Button_Img = formbase_button.Button_Img;
                    base_button.Button_Type = formbase_button.Button_Type;
                    base_button.Button_Sort = formbase_button.Button_Sort;
                    base_button.Button_Remark = formbase_button.Button_Remark;
                    base_button.Menu_Id = formbase_button.Menu_Id;
                    base_button.Button_Status = formbase_button.Button_Status;
                    bool result = ibase_buttonService.UpdateEntity(base_button);
                    listMsg.Add(new ModelMessage("ChangeResult", "m001", result ? "保存成功" : "保存失败"));
                }
                catch (Exception e)
                {
                    listMsg.Add(new ModelMessage("ChangeResult", "m002", "保存失败"));
                }
            }
            return Json(listMsg);

        }

        //[HttpPost]
        //[AccountAuth(Roles = "Base_Button_Edit")]
        //public ActionResult ActivateSwich(string primarykeys, string activate = "N") 
        //{ 
        //    ModelMessage modelMessage = new ModelMessage(); 

        //    if (OperatorAvailable(primarykeys, activate)) 
        //    { 
        //        modelMessage.Code = "m001"; 
        //        modelMessage.Message = "修改成功"; 
        //    } 
        //    else 
        //    { 
        //        modelMessage.Code = "m002"; 
        //    } 
        //    return Json(modelMessage, JsonRequestBehavior.AllowGet); 
        //} 

        //private bool OperatorAvailable(string primarykeys, string available) 
        //{ 
        //    List<Guid> listBase_ButtonGUID = primarykeys.Split(',').ToListGuid(); 
        //    List<Base_Button> listBase_Button = ibase_buttonService.LoadEntities(x => listBase_ButtonGUID.Contains(x.Button_Id)).ToList(); 
        //    foreach (var item in listBase_Button) 
        //    { 
        //        item.Button_Status = available; 
        //    } 
        //    int updaterow = ibase_buttonService.UpdateEntity(); 
        //    if (updaterow == 0) 
        //    { 
        //        return false; 
        //    } 
        //    return true; 
        //} 




    }
}


